package com.youhong.teethcare.settingpage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.kongzue.dialog.listener.InputDialogOkButtonClickListener;
import com.kongzue.dialog.v2.DialogSettings;
import com.kongzue.dialog.v2.InputDialog;
import com.youhong.teethcare.BaseActivity;
import com.youhong.teethcare.MainActivity;
import com.youhong.teethcare.ModeIntroduceActivity;
import com.youhong.teethcare.R;
import com.youhong.teethcare.customviews.ImageViewColorCard;
import com.youhong.teethcare.customviews.ImageViewSwitchBar;
import com.youhong.teethcare.dialogs.BrushModeDialog;
import com.youhong.teethcare.services.Common;
import com.youhong.teethcare.utils.AppManager;
import com.youhong.teethcare.utils.ContentResolverUtils;
import com.youhong.teethcare.utils.ImageUtils;
import com.youhong.teethcare.utils.LanguageUtils;
import com.youhong.teethcare.utils.MyCircleHeadView;
import com.youhong.teethcare.utils.ScreenUtils;
import com.youhong.teethcare.utils.Util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class PersonalInfoActivity extends BaseActivity implements View.OnClickListener {

    ImageView iv_back, iv_portrait;
    RelativeLayout rl_photo, rl_nickname, rl_gender, rl_birthday;
    TextView tv_coffee, tv_tea, tv_redwine, tv_smoking, tv_none;
    TextView tv_condition1, tv_condition2, tv_condition3, tv_condition4;
    TextView tv_gender, tv_nickname, tv_birthday;
    TextView tv_left, tv_right;
    RelativeLayout rl_hand;
    Button btn_next;
    TimePickerView pvTime = null;
    ImageViewColorCard iv_colorChart;
    ImageViewSwitchBar iv_switchBar;
    ImageView iv_mouth;
    int textSelectedColor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getInstance().addActivity(this);
        initStatusBar(getResources().getColor(R.color.pageBackground), true);

        setContentView(R.layout.activity_personal_info);
        getViews();
        initializeVariables();
    }

    ImageViewSwitchBar.OnThumbMoveListener onThumbMoveListener = new ImageViewSwitchBar.OnThumbMoveListener() {
        @Override
        public void onThumbMoved(int leftMargin) {
            //SwitchBar 的(LeftMargin + SwitchBar 的一半宽度) -
            //  ColorCard 的leftSpace
            // 就等于指标在ColorCard 的位置
            int position = leftMargin + halfOfSwitchBar - leftSpace;
            Log.e("abc", "Position:" + position);
            if (position >= iv_colorChart.rectf1.left && position <= iv_colorChart.rectf1.right) {

                iv_mouth.setImageDrawable(getResources().getDrawable(R.mipmap.pic_mouth1));


            } else if (position >= iv_colorChart.rectf2.left && position <= iv_colorChart.rectf2.right) {
                iv_mouth.setImageDrawable(getResources().getDrawable(R.mipmap.pic_mouth2));


            } else if (position >= iv_colorChart.rectf3.left && position <= iv_colorChart.rectf3.right) {

                iv_mouth.setImageDrawable(getResources().getDrawable(R.mipmap.pic_mouth3));

            } else if (position >= iv_colorChart.rectf4.left && position <= iv_colorChart.rectf4.right) {

                iv_mouth.setImageDrawable(getResources().getDrawable(R.mipmap.pic_mouth4));

            } else if (position >= iv_colorChart.rectf5.left && position <= iv_colorChart.rectf5.right) {

                iv_mouth.setImageDrawable(getResources().getDrawable(R.mipmap.pic_mouth5));

            } else if (position >= iv_colorChart.rectf6.left && position <= iv_colorChart.rectf6.right) {

                iv_mouth.setImageDrawable(getResources().getDrawable(R.mipmap.pic_mouth6));
            }

            Common.userInfo.setTeethWhiteness(leftMargin);
            Common.userInfo.SyncToLocal(getApplicationContext());

        }
    };
    
    private void getViews() {
        textSelectedColor=getResources().getColor(R.color.colorMainBackground);
        tv_none = findViewById(R.id.personalInfo_tv_none);
        tv_none.setOnClickListener(this);

        iv_mouth = findViewById(R.id.personalInfo_iv_mouth);

        iv_switchBar = findViewById(R.id.personalInfo_iv_switchBar);
        //当指针移动时的事件
        iv_switchBar.setOnThumbMoveListener(onThumbMoveListener);

        iv_colorChart = findViewById(R.id.personalInfo_iv_colorChart);

        rl_nickname = findViewById(R.id.personalInfo_rl_nickname);
        rl_nickname.setOnClickListener(this);

        btn_next = findViewById(R.id.personalInfo_btn_next);
        btn_next.setOnClickListener(this);

        rl_hand = findViewById(R.id.personalInfo_rl_hand);

        tv_left = findViewById(R.id.personalInfo_tv_left);
        tv_left.setOnClickListener(this);

        tv_right = findViewById(R.id.personalInfo_tv_right);
        tv_right.setOnClickListener(this);

        iv_portrait = findViewById(R.id.personalInfo_iv_portrait);

        rl_gender = findViewById(R.id.personalInfo_rl_gender);
        rl_gender.setOnClickListener(this);

        rl_photo = findViewById(R.id.personalInfo_rl_photo);
        rl_photo.setOnClickListener(this);

        tv_gender = findViewById(R.id.personalInfo_tv_gender);

        tv_nickname = findViewById(R.id.personalInfo_tv_nickname);

        tv_birthday = findViewById(R.id.personalInfo_tv_birthday);

        iv_back = findViewById(R.id.personalInfo_iv_back);
        iv_back.setOnClickListener(this);

        tv_coffee = findViewById(R.id.personalInfo_tv_coffee);
        tv_coffee.setOnClickListener(this);

        tv_tea = findViewById(R.id.personalInfo_tv_tea);
        tv_tea.setOnClickListener(this);

        tv_redwine = findViewById(R.id.personalInfo_tv_redwine);
        tv_redwine.setOnClickListener(this);

        tv_smoking = findViewById(R.id.personalInfo_tv_smoking);
        tv_smoking.setOnClickListener(this);


        tv_condition1 = findViewById(R.id.personalInfo_tv_condition1);
        tv_condition1.setOnClickListener(this);


        tv_condition2 = findViewById(R.id.personalInfo_tv_condition2);
        tv_condition2.setOnClickListener(this);

        tv_condition3 = findViewById(R.id.personalInfo_tv_condition3);
        tv_condition3.setOnClickListener(this);

        tv_condition4 = findViewById(R.id.personalInfo_tv_condition4);
        tv_condition4.setOnClickListener(this);

        rl_birthday = findViewById(R.id.personalInfo_rl_birthday);
        rl_birthday.setOnClickListener(this);
    }

    // ColorCard 的12分之1 宽度
    int oneOf12 = 0;
    // ColorCard 居中后，左右俩边任意一边空白的宽度
    int leftSpace = 0;

    //SwitchBar 的一半宽度
    int halfOfSwitchBar = 0;

    @Override
    protected void onStart() {
        super.onStart();
        int screen_width = ScreenUtils.getScreenWidth(getApplicationContext());

        iv_colorChart.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        iv_switchBar.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        int colorChart_width = iv_colorChart.getMeasuredWidth();
        int switchBar_width = iv_switchBar.getMeasuredWidth();
        //ColorCard 左边居中左边空白的宽度。
        leftSpace = (screen_width - colorChart_width) / 2;

        oneOf12 = colorChart_width / 12;
        int minMargin = leftSpace - (switchBar_width / 2) + oneOf12;
        int maxMargin = (screen_width - leftSpace) - (switchBar_width / 2) - oneOf12;


        halfOfSwitchBar = (switchBar_width / 2);
        //计算MinMargin & MaxMargin
        iv_switchBar.setMinLeftMargin(minMargin);
        iv_switchBar.setMaxLeftMargin(maxMargin);


        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) iv_switchBar.getLayoutParams();

                if (Common.userInfo.getTeethWhiteness() != 0) {

                    lp.leftMargin = Common.userInfo.getTeethWhiteness();
                    onThumbMoveListener.onThumbMoved(lp.leftMargin);

                } else {

                    lp.leftMargin = oneOf12;
                    onThumbMoveListener.onThumbMoved(Common.userInfo.getTeethWhiteness());

                }
                iv_switchBar.setLayoutParams(lp);
            }
        }, 300);


    }

    private void initializeVariables() {

        //生日
        Calendar birthday = Common.userInfo.getBirthday();

        if (birthday == null) {
            birthday = Calendar.getInstance();
            birthday.set(1970, 0, 1);
            Common.userInfo.setBirthday(birthday);
        }

        if (Common.languageUtils.getLanguage() == LanguageUtils.L_CHINESE) {

            tv_birthday.setText(Util.FormDateTimeString2(birthday));

        } else {
            tv_birthday.setText(Util.FormDateTimeString3(birthday));
        }
        //昵称
        tv_nickname.setText(Common.userInfo.getNickName());

        //性别
        int gender = Common.userInfo.getGender();

        if (gender == 0) {
            gender = 1;
            Common.userInfo.setGender(gender);
        }

        if (gender == 1) {

            tv_gender.setText(getResources().getString(R.string.strings51));

        } else {

            tv_gender.setText(getResources().getString(R.string.strings52));
        }

        //头像
        showIcon();
//        String potraitPath = Common.userInfo.getHeadUrl();
//        if (TextUtils.isEmpty(potraitPath)) {
//
//            iv_portrait.setImageDrawable(getResources().getDrawable(R.mipmap.icon_head));
//
//        } else {
//
//            /*Glide.with(getApplicationContext()).load(Common.userInfo.getHeadUrl()).asBitmap().;*/
//
//            Bitmap map = BitmapFactory.decodeFile(Common.userInfo.getHeadUrl());
//            iv_portrait.setImageDrawable(new MyCircleHeadView(map));
//        }

        //惯用手
        int hand = Common.userInfo.getHand();

        if (hand == 0) {

            rl_hand.setBackground(getResources().getDrawable(R.mipmap.hand_left));
            tv_right.setTextColor(Color.WHITE);
            tv_left.setTextColor(getResources().getColor(R.color.mainColor));

        } else {
            rl_hand.setBackground(getResources().getDrawable(R.mipmap.hand_right));
            tv_left.setTextColor(Color.WHITE);
            tv_right.setTextColor(getResources().getColor(R.color.mainColor));

        }
        manner = Common.userInfo.getManner();

        if (manner == 0) {

            Drawable drawable = getResources().getDrawable(R.mipmap.icon_none_selected);
            drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

            tv_none.setCompoundDrawables(
                    null, drawable, null, null);
            tv_none.setTextColor(textSelectedColor);

        }

        //习惯
        if ((manner & 0x1) != 0) {

            Drawable drawable = getResources().getDrawable(R.mipmap.icon_coffee_selected);
            drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

            tv_coffee.setCompoundDrawables(
                    null, drawable, null, null);
            tv_coffee.setTextColor(textSelectedColor);
        }

        if ((manner & 0x2) != 0) {

            Drawable drawable = getResources().getDrawable(R.mipmap.icon_tea_selected);
            drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

            tv_tea.setCompoundDrawables(
                    null, drawable, null, null);
            tv_tea.setTextColor(textSelectedColor);
        }

        if ((manner & 0x4) != 0) {

            Drawable drawable = getResources().getDrawable(R.mipmap.icon_redwine_selected);
            drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

            tv_redwine.setCompoundDrawables(
                    null, drawable, null, null);
            tv_redwine.setTextColor(textSelectedColor);

        }

        if ((manner & 0x8) != 0) {

            Drawable drawable = getResources().getDrawable(R.mipmap.icon_smoking_selected);
            drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

            tv_smoking.setCompoundDrawables(
                    null, drawable, null, null);
            tv_smoking.setTextColor(textSelectedColor);

        }

        //牙齿状况
        int condition = Common.userInfo.getCondition();
        conditionClear();
        if (condition == 0) {

            setDrawableTop(tv_condition1, R.mipmap.teethcondition_healthy_selected);
            tv_condition1.setTextColor(textSelectedColor);

        } else if (condition == 1) {

            setDrawableTop(tv_condition2, R.mipmap.teethcondition_sensitive_selected);
            tv_condition1.setTextColor(textSelectedColor);

        } else if (condition == 2) {

            setDrawableTop(tv_condition3, R.mipmap.teethcondition_gumbleeding_selected);
            tv_condition1.setTextColor(textSelectedColor);

        } else if (condition == 3) {

            setDrawableTop(tv_condition4, R.mipmap.teethcondition_yellowish_selected);
            tv_condition1.setTextColor(textSelectedColor);
        }
    }

    private void setDrawableTop(TextView tv, int drawableResId) {

        Drawable drawable = getResources().getDrawable(drawableResId);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        tv.setCompoundDrawables(null, drawable, null, null);

    }

    private void conditionClear() {

        setDrawableTop(tv_condition1, R.mipmap.teethcondition_healthy_unselected);
        tv_condition1.setTextColor(Color.parseColor("#666666"));

        setDrawableTop(tv_condition2, R.mipmap.teethcondition_sensitive_unselected);
        tv_condition2.setBackgroundColor(Color.TRANSPARENT);
        tv_condition2.setTextColor(Color.parseColor("#666666"));

        setDrawableTop(tv_condition3, R.mipmap.teethcondition_gumbleeding_unselected);
        tv_condition3.setBackgroundColor(Color.TRANSPARENT);
        tv_condition3.setTextColor(Color.parseColor("#666666"));

        setDrawableTop(tv_condition4, R.mipmap.teethcondition_yellowish_unselected);
        tv_condition4.setBackgroundColor(Color.TRANSPARENT);
        tv_condition4.setTextColor(Color.parseColor("#666666"));
    }

    private void habitClear() {
        {
            //Coffee
            Drawable drawable = getResources().getDrawable(R.mipmap.icon_coffee_unselected);
            drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

            tv_coffee.setCompoundDrawables(
                    null, drawable, null, null);
            tv_coffee.setTextColor(Color.parseColor("#d8d8d8"));

            //Clear bit-0
            manner &= 0xFE;
        }

        //Tea
        {
            Drawable drawable = getResources().getDrawable(R.mipmap.icon_tea_unselected);
            drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

            tv_tea.setCompoundDrawables(
                    null, drawable, null, null);
            tv_tea.setTextColor(Color.parseColor("#d8d8d8"));
            //Clear bit-1
            manner &= 0xFD;
        }

        //redwine
        {
            Drawable drawable = getResources().getDrawable(R.mipmap.icon_redwine_unselected);
            drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

            tv_redwine.setCompoundDrawables(
                    null, drawable, null, null);
            tv_redwine.setTextColor(Color.parseColor("#d8d8d8"));
            //Clear bit-2
            manner &= 0xFB;
        }

        //Smoking
        {
            Drawable drawable = getResources().getDrawable(R.mipmap.icon_smoke_unselected);
            drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

            tv_smoking.setCompoundDrawables(
                    null, drawable, null, null);
            tv_smoking.setTextColor(Color.parseColor("#d8d8d8"));


            //Clear bit-3
            manner &= 0xF7;

        }
    }

    private void noneClear() {

        Drawable drawable = getResources().getDrawable(R.mipmap.icon_none_unselected);
        drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

        tv_none.setCompoundDrawables(
                null, drawable, null, null);
        tv_none.setTextColor(Color.parseColor("#d8d8d8"));
    }

    private void noneShow() {

        if (manner == 0) {

            Drawable drawable = getResources().getDrawable(R.mipmap.icon_none_selected);
            drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

            tv_none.setCompoundDrawables(
                    null, drawable, null, null);
            tv_none.setTextColor(textSelectedColor);

        }

    }

    int manner = 0x1;

    private static final int REQ_GENDER = 0x1;
    private static final int REQ_PORTRAIT = 0x2;


    @Override
    public void onClick(View v) {

        if (iv_back == v) {

            AppManager.getInstance().killTopActivity();

        } else if (v == tv_none) {

            habitClear();

            Drawable drawable = getResources().getDrawable(R.mipmap.icon_none_selected);
            drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

            tv_none.setCompoundDrawables(
                    null, drawable, null, null);
            tv_none.setTextColor(textSelectedColor);

            manner = 0;

            Common.userInfo.setManner(manner);
            Common.userInfo.SyncToLocal(getApplicationContext());

        } else if (v == tv_coffee) {


            if ((manner & 0x1) != 0) {

                Drawable drawable = getResources().getDrawable(R.mipmap.icon_coffee_unselected);
                drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

                tv_coffee.setCompoundDrawables(
                        null, drawable, null, null);
                tv_coffee.setTextColor(Color.parseColor("#d8d8d8"));

                //Clear bit-0
                manner &= 0xFE;
                noneShow();

            } else {

                noneClear();

                Drawable drawable = getResources().getDrawable(R.mipmap.icon_coffee_selected);
                drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

                tv_coffee.setCompoundDrawables(
                        null, drawable, null, null);
                tv_coffee.setTextColor(textSelectedColor);
                manner |= 0x1;

            }

            Common.userInfo.setManner(manner);
            Common.userInfo.SyncToLocal(getApplicationContext());


        } else if (v == tv_tea) {

            if ((manner & 0x2) != 0) {

                Drawable drawable = getResources().getDrawable(R.mipmap.icon_tea_unselected);
                drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

                tv_tea.setCompoundDrawables(
                        null, drawable, null, null);
                tv_tea.setTextColor(Color.parseColor("#d8d8d8"));
                //Clear bit-1
                manner &= 0xFD;
                noneShow();
            } else {
                noneClear();
                Drawable drawable = getResources().getDrawable(R.mipmap.icon_tea_selected);
                drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

                tv_tea.setCompoundDrawables(
                        null, drawable, null, null);
                tv_tea.setTextColor(textSelectedColor);
                manner |= 0x2;
            }

            Common.userInfo.setManner(manner);
            Common.userInfo.SyncToLocal(getApplicationContext());

        } else if (v == tv_redwine) {

            if ((manner & 0x4) != 0) {

                Drawable drawable = getResources().getDrawable(R.mipmap.icon_redwine_unselected);
                drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

                tv_redwine.setCompoundDrawables(
                        null, drawable, null, null);
                tv_redwine.setTextColor(Color.parseColor("#d8d8d8"));
                //Clear bit-2
                manner &= 0xFB;
                noneShow();

            } else {

                noneClear();
                Drawable drawable = getResources().getDrawable(R.mipmap.icon_redwine_selected);
                drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

                tv_redwine.setCompoundDrawables(
                        null, drawable, null, null);
                tv_redwine.setTextColor(textSelectedColor);
                manner |= 0x4;


            }

            Common.userInfo.setManner(manner);
            Common.userInfo.SyncToLocal(getApplicationContext());


        } else if (v == tv_smoking) {

            if ((manner & 0x8) != 0) {

                Drawable drawable = getResources().getDrawable(R.mipmap.icon_smoke_unselected);
                drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

                tv_smoking.setCompoundDrawables(
                        null, drawable, null, null);
                tv_smoking.setTextColor(Color.parseColor("#d8d8d8"));


                //Clear bit-3
                manner &= 0xF7;
                noneShow();
            } else {
                noneClear();

                Drawable drawable = getResources().getDrawable(R.mipmap.icon_smoking_selected);
                drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

                tv_smoking.setCompoundDrawables(
                        null, drawable, null, null);
                tv_smoking.setTextColor(textSelectedColor);

                manner |= 0x8;
            }

            Common.userInfo.setManner(manner);
            Common.userInfo.SyncToLocal(getApplicationContext());

        } else if (v == tv_condition1) {

            conditionClear();
            setDrawableTop(tv_condition1, R.mipmap.teethcondition_healthy_selected);
            tv_condition1.setTextColor(textSelectedColor);
            Common.userInfo.setCondition(0);

        } else if (v == tv_condition2) {
            conditionClear();

            setDrawableTop(tv_condition2, R.mipmap.teethcondition_sensitive_selected);
            tv_condition2.setTextColor(textSelectedColor);
            Common.userInfo.setCondition(1);

        } else if (v == tv_condition3) {

            conditionClear();
            setDrawableTop(tv_condition3, R.mipmap.teethcondition_gumbleeding_selected);
            tv_condition3.setTextColor(textSelectedColor);
            Common.userInfo.setCondition(2);

        } else if (v == tv_condition4) {

            conditionClear();
            setDrawableTop(tv_condition4, R.mipmap.teethcondition_yellowish_selected);
            tv_condition4.setTextColor(Color.parseColor("#05A0FF"));
            Common.userInfo.setCondition(3);


        } else if (rl_birthday == v) {

            Calendar current_birthday = Common.userInfo.getBirthday();

            pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelect(Date date, View v) {
                    Calendar c = Calendar.getInstance();
                    c.setTime(date);
                    if (Common.languageUtils.getLanguage() == LanguageUtils.L_CHINESE) {
                        tv_birthday.setText(Util.FormDateTimeString2(c));
                    } else {
                        tv_birthday.setText(Util.FormDateTimeString3(c));
                    }
                    Common.userInfo.setBirthday(c);
                    Common.userInfo.SyncToLocal(getApplicationContext());
                }
            }).setLayoutRes(R.layout.customer_date_pickerview, new CustomListener() {
                @Override
                public void customLayout(View v) {
                    final Button tvSubmit = (Button) v.findViewById(R.id.btnSubmit);
                    Button ivCancel = (Button) v.findViewById(R.id.btnCancel);
                    tvSubmit.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            pvTime.returnData();
                            pvTime.dismiss();
                        }
                    });
                    ivCancel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            pvTime.dismiss();
                        }
                    });
                }
            })

                    .setType(new boolean[]{true, true, true, false, false, false})
                    .setTitleBgColor(getResources().getColor(R.color.mainColor))
                    .setSubmitColor(getResources().getColor(R.color.white))
                    .setCancelColor(getResources().getColor(R.color.white))
                    .setTextColorCenter(getResources().getColor(R.color.mainColor))
                    .setTitleText(getResources().getString(R.string.strings38))
                    .setItemVisibleCount(7)
                    .setTitleColor(getResources().getColor(R.color.white))
                    .setSubmitText(getResources().getString(R.string.strings103))
                    .setCancelText(getResources().getString(R.string.LogOut_tv_cancel))
                    .setLineSpacingMultiplier(3)
                    .setDate(current_birthday)
                    .setLabel("", "", "", "", "", "")
                    .build();

            pvTime.show();
        } else if (rl_gender == v) {
           showGenderDialog();
        } else if (rl_photo == v) {

            Intent intent = new Intent(getApplicationContext(), PortraitSelectActivity.class);
            startActivityForResult(intent, REQ_PORTRAIT);

        } else if (tv_left == v) {


            rl_hand.setBackground(getResources().getDrawable(R.mipmap.hand_right));
            tv_left.setTextColor(Color.WHITE);
            tv_right.setTextColor(textSelectedColor);

            if (MainActivity.mDevice != null) {

                MainActivity.mDevice.setHand((byte) 0x1);
            }

            Common.userInfo.setHand(1);
            Common.userInfo.SyncToLocal(getApplicationContext());


        } else if (tv_right == v) {

            rl_hand.setBackground(getResources().getDrawable(R.mipmap.hand_left));
            tv_right.setTextColor(Color.WHITE);
            tv_left.setTextColor(textSelectedColor);

            if (MainActivity.mDevice != null) {
                MainActivity.mDevice.setHand((byte) 0x2);
            }


            Common.userInfo.setHand(0);
            Common.userInfo.SyncToLocal(getApplicationContext());

        } else if (btn_next == v) {

            Intent intent = new Intent(PersonalInfoActivity.this, ModeIntroduceActivity.class);
            startActivity(intent);

        } else if (rl_nickname == v) {

            DialogSettings.type = DialogSettings.TYPE_MATERIAL;
            InputDialog.show(PersonalInfoActivity.this, getString(R.string.strings94),
                    getResources().getString(R.string.strings93),
                    getResources().getString(R.string.strings103), new InputDialogOkButtonClickListener() {
                        @Override
                        public void onClick(Dialog dialog, String inputText) {

                            //当输入的Nickname 不为空时
                            if (!TextUtils.isEmpty(inputText)) {

                                byte[] bytes = inputText.getBytes();

                                inputText = inputText.replaceAll("\\u000A", "");
                                inputText.trim();

                                Common.userInfo.setNickName(inputText);
                                tv_nickname.setText(inputText);
                                Common.userInfo.SyncToLocal(getApplicationContext());

                            }
                        }
                    },
                    getResources().getString(R.string.LogOut_tv_cancel), null
            );
        }
    }

    private void showGenderDialog() {
        int gender = Common.userInfo.getGender();
        if (gender == 0) {
            gender = 1;
        }
        List<String> list = new ArrayList<>();
        list.add(getResources().getString(R.string.strings51));
        list.add(getResources().getString(R.string.strings52));
        BrushModeDialog brushModeDialog = BrushModeDialog.newInstance(BrushModeDialog.Type_Gender, gender-1);
        brushModeDialog.setData(list);
        brushModeDialog.setLimitValueListener(new BrushModeDialog.LimitValueListener() {
            @Override
            public void limitValueCallBack(int type, int lowValue) {
                tv_gender.setText(list.get(lowValue));
                Common.userInfo.setGender(lowValue+1);
                Common.userInfo.SyncToLocal(getApplicationContext());
            }
        });
        brushModeDialog.show(getSupportFragmentManager(), "");
    }

    private static final String TAG = "PersonalInfoActivity";
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == REQ_GENDER && resultCode == Activity.RESULT_OK) {

            String gender = data.getStringExtra("gender");
            int stringid = gender.equals("1") ? R.string.strings51 : R.string.strings52;
            String str = getResources().getString(stringid);
            tv_gender.setText(str);
            Common.userInfo.setGender(Integer.parseInt(gender));
            Common.userInfo.SyncToLocal(getApplicationContext());
        } else if (requestCode == REQ_PORTRAIT && resultCode == Activity.RESULT_OK) {

            Uri uri = data.getData();
            Log.i(TAG, "onActivityResult: "+uri.toString());
            Common.userInfo.setHeadUrl(uri.toString());
            Common.userInfo.SyncToLocal(getApplicationContext());
            showIcon();
//            Bitmap map = BitmapFactory.decodeFile(Common.userInfo.getHeadUrl());
//            //Bitmap map = ContentResolverUtils.decodeUriAsBitmap(uri,this);
//            iv_portrait.setImageDrawable(new MyCircleHeadView(map));
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
    void showIcon(){
        ImageUtils.showIcon(iv_portrait);
    }
}
